<template>
  <div>
    <TopFilter
      ref="topFilter"
      :list="topFilterList"
      :cols="3"
      labelWidth="90"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :datakey="'records'"
      :height="'auto'"
      :isSelectColumn="false"
      :isToperInfo="false"
      columnsRef="ysjSales2019092501Table"
      :onColumnsChange="columns => (this.columns = columns)"
    ></FilterTable>

    <Drawer
      :visible.sync="visible"
      :title="(type == 'add' ? '关爱' : '关爱明细')"
      :width="1200"
      destroyOnClose
      :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"
    >
      <SalesVisitDetail
        :showChildren="showChildren"
        :customerBusinessId="customerBusinessId"
        :type="type"
        :careStatus="careStatus"
        :requiredDate="requiredDate"
        :returnVisitContent="returnVisitContent"
        :customerCareId="customerCareId"
        :recordVersion="recordVersion"
        :careTargetId="careTargetId"
        :employeeId="employeeId"
        :billId="id"
        @changeHandle="changeHandle"
        @close="closeHandler"
      />
    </Drawer>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { formatdate } from '@/utils/index';
import { getCareObjList } from '@/api/customerCare/saleVisit/salesVisit';
import { getUserInfoByRole, getUserInfo } from '@/api/customerCare/saleVisit/careTaskSearch';
import SalesVisitDetail from './salesVisitDetail';

export default {
  name: 'SalesVisit',
  mixins: [dictionary],
  components: { SalesVisitDetail },
  data() {
    this.BaseTable = null;
    return {
      columns: this.createTableColumns(),
      topFilterList: this.createTopFilters(),
      fetchapi: getCareObjList,
      fetchParams: {
        careStatusList: '15921001,15921002'
      },
      type: '',
      visible: false,
      recordVersion: '',
      employeeId: '',
      id: '',
      careTargetId: '',
      returnVisitContent: '',
      customerCareId: '',
      requiredDate: '',
      careStatus: 0,
      customerBusinessId: '',
      showChildren: true
    };
  },
  methods: {
    createTopFilters() {
      return [
        {
          type: 'MULTIPLE_SELECT',
          label: '关爱状态',
          fieldName: 'careStatusList',
          placeholder: '请选择...',
          itemList: this.createDictList('1592'),
          initialValue: [15921001, 15921002]
        },
        {
          type: 'SELECT',
          label: '关爱人员',
          fieldName: 'careEmpId',
          placeholder: '请选择...',
          itemList: this.createdEmpList()
        },
        {
          type: 'INPUT',
          label: '客户编号',
          fieldName: 'customerNo',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'customerName',
          placeholder: '请输入...'
        },
        {
          type: 'RANGE_DATE',
          label: '出库日期',
          fieldName: 'stockInOutDateStart|stockInOutDateEnd',
          placeholder: '出库日期'
        },
        {
          type: 'RANGE_DATE',
          label: '要求完成日期',
          fieldName: 'requiredDateStart|requiredDateEnd',
          placeholder: '要求完成日期'
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: 'VIN'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          align: 'center',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 115,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" disabled={props.row.careStatus == 15921003 || props.row.careStatus == 15921004} type="text" onClick={() => this.careTask(props.row)}>
                  关爱
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" onClick={() => this.careTaskDetail(props.row)}>
                  关爱明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '关爱人员',
          dataIndex: 'employeeName',
          filterType: 'input'
        },
        {
          title: '车型',
          dataIndex: 'modelName',
          filterType: 'input'
        },
        {
          title: '销售顾问',
          dataIndex: 'consultant',
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'customerName',
          filterType: 'input'
        },
        {
          title: '客户电话',
          dataIndex: 'phone',
          filterType: 'input'
        },
        {
          title: '性别',
          dataIndex: 'gender',
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.gender, '1002')}</div>;
          }
        },
        {
          title: '最迟关爱日期',
          dataIndex: 'requiredDate',
          filterType: 'input',
          render: props => {
            if (props.row.requiredDate && props.row.requiredDate != '') {
              return <div>{formatdate(new Date(parseInt(props.row.requiredDate)), 1)}</div>;
            }
          }
        },
        {
          title: '关爱状态',
          dataIndex: 'careStatus',
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.careStatus, '1592')}</div>;
          }
        },
        {
          title: '回访结果',
          dataIndex: 'careResult',
          filterType: 'input',
          render: props => {
            if (props.row.careStatus == '15921001') {
              return <div></div>;
            } else if (props.row.careStatus == '15921002'||props.row.careStatus == '15921003') {
              return <div>正常</div>;
            } else if (props.row.careStatus == '15921004') {
              return <div>{props.row.dissatisfiedCause}</div>;
            }
          }
        },
        {
          title: '总体满意度',
          dataIndex: 'satisfaction',
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.satisfaction, '1075')}</div>;
          }
        },
        {
          title: '不满意原因',
          dataIndex: 'basedataName',
          filterType: 'input'
        },
        {
          title: '下次关爱日期',
          dataIndex: 'nextCareDate',
          filterType: 'input',
          render: props => {
            if (props.row.nextCareDate && props.row.nextCareDate != '') {
              return <div>{formatdate(new Date(parseInt(props.row.nextCareDate)), 1)}</div>;
            }
          }
        },
        {
          title: '关爱内容',
          dataIndex: 'careContent',
          filterType: 'input'
        },
        {
          title: '订单日期',
          dataIndex: 'orderAtDate',
          filterType: 'input',
          render: props => {
            if(props.row.orderAtDate&&props.row.orderAtDate!=''){
              return <div>{formatdate(new Date(parseInt(props.row.orderAtDate)), 1)}</div>;
            }
          }
        },
        {
          title: '出库日期',
          dataIndex: 'stockInOutDate',
          filterType: 'input',
          render: props => {
            if (props.row.stockInOutDate && props.row.stockInOutDate != '') {
              return <div>{formatdate(new Date(parseInt(props.row.stockInOutDate)), 1)}</div>;
            }
          }
        },
        {
          title: '品牌',
          dataIndex: 'brandName',
          filterType: 'input'
        },
        {
          title: '车系',
          dataIndex: 'seriesName',
          filterType: 'input'
        // },
        // {
        //   title: 'VIN',
        //   dataIndex: 'salesVin',
        //   width: 150,
        //   filterType: 'input'
        }
      ];
    },
    createdEmpList() {
      //初始化关爱人员选择列表
      getUserInfo().then(res => {
        if (res.resultCode == 200) {
          let param = {
            companyId: res.data.userInfo.companyId,
            positionCode: '10061010'
          };
          getUserInfoByRole(param).then(res => {
            if (res.resultCode == 200) {
              this.topFilterList[1].itemList = res.data.map(x => ({ value: x.userId, text: x.employeeName }));
            } else {
              this.$message.error('获取关爱人员列表失败!');
            }
          });
        } else {
          this.$message.error('获取当前登录用户信息失败!');
        }
      });
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    careTask(row) {
      this.type = 'add';
      this.visible = true;
      this.id = row.billId + '';
      this.employeeId = row.employeeId + '';
      this.careTargetId = row.careTargetId + '';
      (this.careStatus = row.careStatus), (this.returnVisitContent = row.returnVisitContent + '');
      this.customerCareId = row.customerCareId + '';
      this.recordVersion = row.recordVersion + '';
      this.customerBusinessId = row.customerBusinessId;
      this.requiredDate = formatdate(new Date(parseInt(row.requiredDate)), 1);
    },
    careTaskDetail(row) {
      this.type = 'show';
      this.visible = true;
      this.id = row.billId + '';
      this.employeeId = row.employeeId + '';
      this.careTargetId = row.careTargetId + '';
      (this.careStatus = row.careStatus), (this.returnVisitContent = row.returnVisitContent + '');
      this.customerCareId = row.customerCareId + '';
      this.recordVersion = row.recordVersion + '';
      this.customerBusinessId = row.customerBusinessId;
      this.requiredDate = formatdate(new Date(parseInt(row.requiredDate)), 1);
    }
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  }
};
</script>
